﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Entity;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmMyIncome : FrmBase
    {
        private string m_incomeType;

        public FrmMyIncome(string incomeType)
        {
            InitializeComponent();
            m_incomeType = incomeType;
            ClearControl();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (!base.CheckLogin())
                return;

            ClearControl();

            new Thread(() =>
            {
                try
                {
                    QttIncomeInfo infos = null;
                    int pageNum = 1;
                    for (int i = 0; ; i++)
                    {
                        var info = QttHelper.GetIncomes(base.MemeberToken, m_incomeType, pageNum++, 20);
                        if (info.Data.List.Count < 1)
                            break;
                        if (infos!=null)
                        {
                            infos.List.AddRange(info.Data.List); 
                        }
                        else
                        {
                            infos = info.Data;
                        }
                    }
                    base.InvokeCrossThread<QttIncomeInfo>(Info2Controls, infos);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }).Start();
        }

        private void ClearControl()
        {
            this.listView1.Items.Clear();
            this.label1.Text = "";
            this.label2.Text = "";
            this.linkLabel1.Text = "";
            this.linkLabel1.Tag = null;
        }

        private void Info2Controls(QttIncomeInfo infos)
        {
            int index = 0;
            foreach (var info in infos.List)
            {
                this.listView1.Items.Add(new ListViewItem(new string[]
                {
                    (++index).ToString(),
                    info.Name,
                    info.Description,
                    info.Amount,
                    info.CreateTime
                }));
            }

            this.label1.Text = string.Format("昨天{0}收入：{1}", 
                this.m_incomeType == "coins"?"金币":"零钱",
                infos.Yesterday);
            this.label2.Text = string.Format("昨天金币转换率：{0}金币={1}元，即{2}金币=1元",
                infos.Convert.Coins,
                infos.Convert.Balance,
                (Convert.ToInt32(infos.Convert.Coins)*1.0/Convert.ToDouble(infos.Convert.Balance)).ToString("0."));

            if (this.m_incomeType == "coins")
            {
                int todayCoins = 0;
                var date = DateTime.Now;
                var today = new DateTime(date.Year, date.Month, date.Day).ToString("yyyy-MM-dd HH:mm:ss");
                var todayStatistics = new Dictionary<string,double>();
                foreach (var info in infos.List)
                {
                    if (info.CreateTime.CompareTo(today) > 0)
                    {
                        if (info.Amount.Contains("-"))
                            continue;

                        var amount = Convert.ToInt32(info.Amount);
                        todayCoins += amount;
                        if (!todayStatistics.ContainsKey(info.Name))
                        {
                            todayStatistics[info.Name] = 0d;
                        }
                        todayStatistics[info.Name] += amount;
                    }
                }
                this.linkLabel1.Text = "今日总收入：" + todayCoins;
                this.linkLabel1.Tag = todayStatistics;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var dic = this.linkLabel1.Tag as Dictionary<string,double>;
            if (dic != null)
            {
                var frm = new FrmIncomeStatistics(dic);
                frm.Text = "今日收入明细";
                var point = this.linkLabel1.Location;
                point.Y += this.linkLabel1.Height;
                point = this.PointToScreen(point);
                frm.Location = point;
                frm.Show();
            }
        }
    }
}
